package com.it.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.it.bean.Question;
import com.it.mapper.QuestionMapper;
import com.it.page.PageQuery;
import com.it.service.QuestionService;
import com.it.utils.AppUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("questionService")
@RequiredArgsConstructor
public class QuestionServiceImpl implements QuestionService {

    private final QuestionMapper questionMapper;

    @Override
    public PageInfo<Question> page(PageQuery<Question> pageQuery) {
        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize());
        Question condition = pageQuery.getQueryBody();
        condition.setDeleted(false);
        List<Question> questions = questionMapper.select(condition);
        return new PageInfo<>(questions);
    }

    @Override
    public void delete(Long id) {
        Question question = questionMapper.selectByPrimaryKey(id);
        question.setDeleted(true);
        AppUtils.forUpdateAudit(question);
        questionMapper.updateByPrimaryKeySelective(question);
    }

    @Override
    public void save(Question question) {
        if (question.getId() == null) {
            AppUtils.forSaveAudit(question);
            questionMapper.save(question);
        } else {
            AppUtils.forUpdateAudit(question);
            questionMapper.updateByPrimaryKeySelective(question);
        }
    }
}
